# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
===========================
@Time : 2022/8/19 0019 21:38
@Author : 测试开发工程师
@File : 4 - flask_param_demo.py
@Software: PyCharm
============================
"""

# 需要导入 request 对象，不是 requests!!!
from flask import Flask, request
from logger_util import logger

app = Flask(__name__)

'https://ceshiren.com/search?q=ios'

# get 请求
@app.route("/login/", methods=['get'])
def login():
    logger.info(f"请求参数为：{request.args}")
    result = request.args
    a = result.get('a')
    b = result.get('b')
    print(f"a = {a},b = {b}")
    return {"code": 0, "msg": "get success"}


if __name__ == '__main__':
    app.run()

# post 调用
"""
{
    "username":"hogwarts"
}
"""

# post 请求
@app.route("/regist/", methods=["post"])
def post_regist():
    logger.info(request.json)
    return {"code": 0, "msg": "post success"}


if __name__ == '__main__':
    app.run()


"""
"name":"hogwarts",
"password":"1230",
"password_confirm":"123",
"email":"214829112@qq.com"

"""


# # 处理前端发来的 form
# 注册 ： 用户名，密码，密码确定，邮箱
@app.route("/regist1/", methods=["put"])
def regist1():
    name = request.form.get('name')
    password = request.form.get('password')
    password_confirm = request.form.get('password_confirm')
    email = request.form.get('email')
    logger.info(request.form)
    logger.info(f"注册的用户信息为：name:{name},password:{password},password_confirm:{password_confirm}，email：{email}")
    return {"code": 0, "msg": "put success"}


if __name__ == '__main__':
    app.run()

# 处理前端发来的文件请求
@app.route("/file/", methods=["post"])
def post_file():
    logger.info(f'请求的方式为:{request.method}')
    logger.info(f'请求的URL为:{request.url}')
    file_object = request.files.get('file')
    logger.info(file_object)
    filename = file_object.filename
    logger.info(f'文件名为：{filename}')
    file_object.save('./logo1.png')
    return {"code": 0, "msg": "post success"}


if __name__ == '__main__':
    app.run()
